package org.jground.rx.ui.zk.viewControl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jground.rx.IRXAccess;
import org.jground.rx.IRXObject;
import org.jground.rx.RXAccess;
import org.jground.rx.RXControl;
import org.jground.rx.RXView;
import org.jground.rx.ui.IUIAttribute;
import org.jground.rx.ui.zk.UIFrame;
import org.jground.rx.ui.zk.UIStyles;
import org.jground.util.ArrayUtils;
import org.jground.zk.zulX.DivX;
import org.jground.zk.zulX.VboxX;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Button;

//
// *** NOT TESTED *** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
public class UIAttributeOneof extends UIAttributeBase implements EventListener {

    private static Log logger = LogFactory.getLog(UIAttributeOneof.class);

    private static final String attKey = "attKey";

    public UIAttributeOneof createNew(UIFrame uiFrame, RXView moControl) {
        return new UIAttributeOneof(uiFrame, moControl);
    }

    //
    public UIAttributeOneof() {}

    private VboxX navigationVbox;
    private VboxX selectedAttributeVbox;
    //
    private VboxX selectionButtonsVbox;
    private Button changeSelectionBt;
    private DivX selectOneLabelDIV;
    //
    private List<IUIAttribute<Component,UIFrame>> subAttributes = new ArrayList<IUIAttribute<Component,UIFrame>>();
    private IUIAttribute<Component,UIFrame> selectedAttribute;

    private boolean isControl;

    private UIAttributeOneof(UIFrame uiFrame, RXView rxView) {
        super(uiFrame, rxView);
        super.uiAttributeView = null;
        // this.uiFrame = uiFrame;
        this.moView = rxView;

        // RXTypeOneOf oneOfType = (RXTypeOneOf)
        // moView.getMOAttribute().getMOType();
        isControl = rxView instanceof RXControl;

        //
        IRXAccess rxAccess = RXAccess.getInstance();
        for (RXView subView : rxView.getAllSubViews()) {
            if (rxAccess.isEntitled(rxView)) {
                IUIAttribute<Component,UIFrame> uiAttribute = UIAttributeFactory.create(uiFrame, subView);
                subAttributes.add(uiAttribute);
            } else {
                logger.warn("No entitlement for " + rxView);
            }
        }
        //
        navigationVbox = new VboxX();
        selectedAttributeVbox = new VboxX();
        selectedAttributeVbox.setSclass(UIStyles.GROUP);
        if (isControl) {
            //
            selectOneLabelDIV = new DivX("Select One");
            changeSelectionBt = new Button("Change Selection");
            changeSelectionBt.addEventListener(Events.ON_CLICK, this);
            //
            selectionButtonsVbox = new VboxX();
            for (RXView subView : rxView.getAllSubViews()) {
                Button selectionButton = new Button(subView.getRXAttribute().getLabel());
                selectionButton.setAttribute(attKey, subView.getRXAttribute().getName());

                selectionButton.addEventListener(Events.ON_CLICK, this);

                selectionButtonsVbox.add(selectionButton);
            }
        }
        super.addToControlComponent(selectedAttributeVbox);

        super.addToLabelComponent(navigationVbox);

    }

    public void set(String... values) {
        clearUI();
        if (ArrayUtils.isNotEmpty(values)) {
            String attref = values[0];
            selectedAttribute = getLocalUIAttribute(attref);
            if (selectedAttribute == null) {
                return;
            }
            displaySelectedAttribute();
        }
    }

    private void displaySelectedAttribute() {
        if (selectedAttribute == null) {
            clearUI();
        }
        if (isControl) {
            navigationVbox.set(changeSelectionBt);
        } else {
            navigationVbox.removeAll();
        }
        selectedAttributeVbox.set(selectedAttribute.getLabelComponent(), selectedAttribute.getControlComponent());
    }

    private IUIAttribute<Component,UIFrame> getLocalUIAttribute(String attref) {
        for (IUIAttribute<Component,UIFrame> uiAttribute : subAttributes) {
            if (uiAttribute.getRXView().getRXAttribute().getName().equals(attref)) {
                return uiAttribute;
            }
        }
        return null;
    }

    public IRXObject updateData() {
        if (isControl) {
            String attref = null;
            if (selectedAttribute != null) {
                attref = selectedAttribute.getRXView().getRXAttribute().getName();
                selectedAttribute.updateData();
                super.moDataObject.set(super.rxAttribute.getName(), attref);
            }
        }
        return super.moDataObject;
    }

    public void clearData() {
        super.clearData();
        for (IUIAttribute<Component,UIFrame> attribute : subAttributes) {
            attribute.clearData();
        }
        clearUI();
    }

    private void clearUI() {
        if (isControl) {
            navigationVbox.removeAll();
            navigationVbox.add(selectOneLabelDIV);
            navigationVbox.add(selectionButtonsVbox);
        } else {
            navigationVbox.removeAll();
        }
        selectedAttributeVbox.removeAll();
    }

    public void setData(IRXObject moDataObject) {
        for (IUIAttribute<Component,UIFrame> attribute : subAttributes) {
            attribute.setData(moDataObject);
        }
        super.setData(moDataObject);
    }

    public void onEvent(Event event) throws Exception {
        if (event.getTarget() == changeSelectionBt) {
            clearUI();
            return;
        }
        selectedAttribute = getLocalUIAttribute((String) event.getTarget().getAttribute(attKey));
        displaySelectedAttribute();
    }

}